package com.szqhyxj.youxiangju.biz.partnerHome.equipment.subordinateList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.SubordinateInfo;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SubordinateInfoActivity extends BaseActivity implements SubordinateInfoContract.View, NavigationBar.NavigationBarListener, MsgChoicePopView.OnClickListener{

    private static final String DEVICEID = "deviceId";
    private static final String BRAND_NAME = "brandName";

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.info_list)
    ListView infoList;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.submit_line)
    LinearLayout submitLine;
    @BindView(R.id.lack)
    LinearLayout lack;
    @BindDrawable(R.mipmap.xuanzhong_15)
    Drawable draw;
    @BindDrawable(R.drawable.background__yuan)
    Drawable circle;
    private List<SubordinateInfo> subordinateInfoList;
    private SubordinateInfoPresenter presenter;
    private String deviceId;
    private SubordinateInfoAdapter adapter;
    private String brandName;
    private int presentIndex = -1;

    private ImageView beforeImg;
    public static void start(Activity activity, String brandName,String deviceId){
        Intent intent = new Intent();
        intent.setClass(activity, SubordinateInfoActivity.class);
        intent.putExtra(DEVICEID, deviceId);
        intent.putExtra(BRAND_NAME, brandName);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subordinate_info);
        ButterKnife.bind(this);
        deviceId = getIntent().getStringExtra(DEVICEID);
        brandName = getIntent().getStringExtra(BRAND_NAME);
        navigationBar.setNavigationBarListener(this);
        subordinateInfoList = new ArrayList<>();
        adapter = new SubordinateInfoAdapter(subordinateInfoList,circle,draw);
        infoList.setAdapter(adapter);
        presenter = new SubordinateInfoPresenter(this);
        presenter.init();

        initEvent();
    }

    private void initEvent(){
        infoList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                presentIndex = i;
                if(beforeImg!=null){
                    beforeImg.setImageDrawable(circle);
                }
                ImageView determine= view.findViewById(R.id.determine);
                beforeImg = determine;
                beforeImg.setImageDrawable(draw);
            }
        });
    }

    @Override
    public void showInfoList(List<SubordinateInfo> subordinateInfoList){
        lack.setVisibility(View.GONE);
        infoList.setVisibility(View.VISIBLE);
        submitLine.setVisibility(View.VISIBLE);
        this.subordinateInfoList.clear();
        this.subordinateInfoList.addAll(subordinateInfoList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showSearchInfo(List<SubordinateInfo> info){
        lack.setVisibility(View.GONE);
        infoList.setVisibility(View.VISIBLE);
        submitLine.setVisibility(View.VISIBLE);
        this.subordinateInfoList.clear();
        this.subordinateInfoList.addAll(subordinateInfoList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showNone(){
        infoList.setVisibility(View.GONE);
        submitLine.setVisibility(View.GONE);
        lack.setVisibility(View.VISIBLE);
    }

    @Override
    public void navSucceed(){
        ConfirmOutSuccessfulActivity.start(this);
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @OnClick({R.id.submit,R.id.search})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.submit:
                if(presentIndex<0){
                    showToast("未选择收件人");
                    return;
                }

                MsgChoicePopView popView = new MsgChoicePopView(this);
                popView.setOnClickListener(this);
                popView.setMsg("下发1台"+brandName+"设备给"+subordinateInfoList.get(presentIndex).getName()+"?");
                popView.show();
                break;
            case R.id.search:
                presenter.searchInfo(name.getText().toString());
                break;
        }
    }

    @Override
    public void onClickConfirm(View v){
        presenter.confirmOut(deviceId,subordinateInfoList.get(presentIndex).getOrganId());
    }
}
